﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;

namespace QuailDashboard.View.Controls
{
    public partial class LogEntriesControl
    {
        private IList _originalItems;
        private int _pageNumber, _itemsPerPage;

        public LogEntriesControl()
        {
            InitializeComponent();
            _itemsPerPage = 50;
        }

        /// <summary>
        /// Gets or sets the number of items per page
        /// </summary>
        public int ItemsPerPage
        {
            get
            {
                return _itemsPerPage;
            }
            
            set
            {
                if (value < 1)
                    throw new ArgumentOutOfRangeException("value");

                _itemsPerPage = value;
                PageNumber = 1;
            }
        }

        /// <summary>
        /// Gets the number of pages
        /// </summary>
        public int PageCount
        {
            get
            {
                if (_originalItems == null)
                    return 1;

                var totalItems = _originalItems.Count;
                var pages = totalItems/ItemsPerPage;
                if (pages == 0 || totalItems % ItemsPerPage != 0)
                    pages++;

                return pages;
            }
        }

        /// <summary>
        /// Gets the items source for the grid log entries
        /// </summary>
        public IList ItemsSource
        {
            get
            {
                return (IList) GridLogEntries.ItemsSource;
            }

            set
            {
                if (value == null || value.Count < ItemsPerPage)
                {
                    GridLogEntries.ItemsSource = value;
                    _originalItems = null;
                    _pageNumber = 1;
                    return;
                }

                _originalItems = value;
                PageNumber = 1;
            }
        }

        /// <summary>
        /// Gets or sets the page number
        /// </summary>
        public int PageNumber
        {
            get
            {
                return _pageNumber;
            }

            set
            {
                if (value < 1)
                    value = 1;
                else if (value > PageCount)
                    value = PageCount;

                _pageNumber = value;

                if (_originalItems == null)
                    return;

                var start = (value - 1)*ItemsPerPage;
                var end = value*ItemsPerPage;

                if (end > _originalItems.Count)
                    end = _originalItems.Count;

                var list = new List<object>();

                for (var i = start; i < end; i++)
                {
                    list.Add(_originalItems[i]);
                }

                GridLogEntries.ItemsSource = list;
            }
        }

        public bool ShowPagingControls
        {
            get
            {
                if (ItemsSource == null)
                    return false;

                return _originalItems != null;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether to show show sync button column.
        /// </summary>
        public bool ShowSyncButton
        {
            get
            {
                return SyncColumn.Visibility == Visibility.Visible;
            }

            set
            {
                SyncColumn.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        public event Action<long> OnSyncToTestRunTaskId;

        private void BtnPreview_Click(object sender, RoutedEventArgs e)
        {
            var b = (Button)sender;
            var tag = b.Tag;

            HtmlPage.Window.Navigate(new Uri("Preview.aspx?entryid=" + tag, UriKind.Relative), "_blank");
        }

        private void BtnSearch_Click(object sender, RoutedEventArgs e)
        {
            var b = (Button) sender;
            var testruntaskid = (long) b.Tag;

            if (OnSyncToTestRunTaskId != null)
                OnSyncToTestRunTaskId(testruntaskid);
        }
    }
}
